/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.chart.editors;

import java.awt.Color;
import java.beans.PropertyEditorSupport;
import java.lang.reflect.Array;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author sammael
 */
public class ChartColorEditorPanel extends javax.swing.JPanel implements ListSelectionListener,DocumentListener{
    private PropertyEditorSupport editor;
    private Color[] colorsArray;
    private String[] stringsArray;
    private ArrayList<Color> colorsList = new ArrayList<>();
    private ArrayList<String> stringsList = new ArrayList<>();
    private DefaultListModel<String> model = new DefaultListModel<>();
    /**
     * Creates new form ChartEditorPanel
     */
    public ChartColorEditorPanel(PropertyEditorSupport editor) {
        initComponents();
        this.editor = editor;
        colorList.setModel(model);
        colorsArray = (Color[])editor.getValue();
        if(colorsArray != null){
            stringsArray = new String[colorsArray.length];
            for(int i =0 ; i<colorsArray.length;++i){
                String str = Integer.toHexString(colorsArray[i].getRGB());
                stringsArray[i] = str;
                stringsList.add(str);
                colorsList.add(colorsArray[i]);
                model.addElement(str);
            }
        }
        colorList.addListSelectionListener(this);
        inputColor.getDocument().addDocumentListener(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        colorList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        inputColor = new javax.swing.JTextField();
        upButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        exampleColor = new com.bean.chart.editors.ColorPanel();
        addButton = new javax.swing.JButton();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Choose Colors for incoming charts");

        colorList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(colorList);

        jLabel2.setText("Color in hex");

        inputColor.setText("000000");

        upButton.setText("Up");
        upButton.setEnabled(false);
        upButton.setPreferredSize(new java.awt.Dimension(60, 27));

        removeButton.setText("Remove");
        removeButton.setEnabled(false);
        removeButton.setPreferredSize(new java.awt.Dimension(60, 27));
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        downButton.setText("Down");
        downButton.setEnabled(false);
        downButton.setPreferredSize(new java.awt.Dimension(60, 27));

        javax.swing.GroupLayout exampleColorLayout = new javax.swing.GroupLayout(exampleColor);
        exampleColor.setLayout(exampleColorLayout);
        exampleColorLayout.setHorizontalGroup(
            exampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 68, Short.MAX_VALUE)
        );
        exampleColorLayout.setVerticalGroup(
            exampleColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exampleColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputColor, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                    .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(88, 88, 88))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(inputColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(exampleColor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
       try{
        Color c = Color.decode("0x"+inputColor.getText());
        model.addElement(inputColor.getText());
        if(colorsArray==null){
            colorsArray = new Color[1];
            stringsArray = new String[1];
            colorsList = new ArrayList<>();
            stringsList = new ArrayList<>();
        }
           colorsList.add(c);
           stringsList.add(inputColor.getText());
           colorsArray = colorsList.toArray(new Color[colorsList.size()]);
           stringsArray = stringsList.toArray(new String[stringsList.size()]);
           if(colorsArray.length > colorsList.size()){
               arrayGrow(colorsArray, colorsList.size());
               arrayGrow(stringsArray, colorsList.size());
           }
           setArray(colorsArray);
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, "Color string is not valid");
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        int index = colorList.getSelectedIndex();
        model.remove(index);
        colorsList.remove(index);
        stringsList.remove(index);
        colorsList.trimToSize();
        stringsList.trimToSize();
        colorsArray = colorsList.toArray(new Color[colorsList.size()]);
        stringsArray = stringsList.toArray(new String[stringsList.size()]);
        arrayGrow(colorsArray, colorsList.size());
        arrayGrow(stringsArray, stringsList.size());
        setArray(colorsArray);
    }//GEN-LAST:event_removeButtonActionPerformed

    private void setArray(Color[] colors){
       editor.setValue(colors); 
    }
    private Object arrayGrow(Object a, int newLength)
   {
      Class cl = a.getClass();
      if (!cl.isArray()) return null;
      Class componentType = a.getClass().getComponentType();
      int length = Array.getLength(a);

      Object newArray = Array.newInstance(componentType, newLength);
      System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength));
      return newArray;
   }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JList colorList;
    private javax.swing.JButton downButton;
    private com.bean.chart.editors.ColorPanel exampleColor;
    private javax.swing.JTextField inputColor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton upButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void valueChanged(ListSelectionEvent arg0) {
        int index = colorList.getSelectedIndex();
        if(index == -1){
            removeButton.setEnabled(false);
            upButton.setEnabled(false);
            downButton.setEnabled(false);
        }else{
            removeButton.setEnabled(true);
            upButton.setEnabled(true);
            downButton.setEnabled(true);
        }
    }

    @Override
    public void insertUpdate(DocumentEvent a) {
        exampleColor.setColor("0x"+inputColor.getText());
    }

    @Override
    public void removeUpdate(DocumentEvent a) {
        exampleColor.setColor("0x"+inputColor.getText());
    }

    @Override
    public void changedUpdate(DocumentEvent a) {
        
    }
}
